package com.jiaquyun.jcps.ui.access

import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import com.jakewharton.rxbinding2.widget.RxTextView
import com.jiaquyun.jcps.R
import com.jiaquyun.jcps.base.BaseAbstractFragment
import io.reactivex.Observable
import io.reactivex.disposables.Disposable
import io.reactivex.functions.Function3
import kotlinx.android.synthetic.main.fragment_access_retrieve.*

class RetrieveFragment : BaseAbstractFragment() {
    override val layoutResId: Int
        get() = R.layout.fragment_access_retrieve


    companion object {
        fun newInstance(): RetrieveFragment {
            return RetrieveFragment()
        }
    }

    override fun initView() {

        observableRetrieve()

        //显示密码
        cbRetrieveEye.setOnCheckedChangeListener { _, isChecked ->
            etRetrievePwd.transformationMethod = if (isChecked)
                HideReturnsTransformationMethod.getInstance()
            else PasswordTransformationMethod.getInstance()
        }
    }


    /**
     * 找回密码输入框监听
     * */
    private fun observableRetrieve() {
        val observablePhone: Observable<CharSequence> =
            RxTextView.textChanges(etRetrievePhone)
//                .doOnNext {
//                    tvRetrieveCodeGet.isEnabled = it.isNotEmpty()
//                }
        val observableCode: Observable<CharSequence> =
            RxTextView.textChanges(etRetrieveCode)
        val observablePwd: Observable<CharSequence> =
            RxTextView.textChanges(etRetrievePwd)
        val observable: Disposable =
            Observable.combineLatest(observablePhone, observableCode, observablePwd,
                Function3<CharSequence, CharSequence, CharSequence, Boolean> { t1, t2, t3 ->
                    t1.isNotEmpty() && t2.isNotEmpty() && t3.isNotEmpty()
                })
                .subscribe {
                    tvRetrieveSubmit.isEnabled = it
                }
        dispose.add(observable)
    }
}